package com.zzyl.nursing.service.impl;

import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.List;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.zzyl.common.core.domain.entity.SysUser;
import com.zzyl.common.core.domain.model.LoginUser;
import com.zzyl.common.utils.SecurityUtils;
import com.zzyl.nursing.dto.AlertDataDto;
import com.zzyl.system.service.ISysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.zzyl.nursing.mapper.AlertDataMapper;
import com.zzyl.nursing.domain.AlertData;
import com.zzyl.nursing.service.IAlertDataService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

/**
 * 报警数据Service业务层处理
 * 
 * @author alexis
 * @date 2024-11-16
 */
@Service
public class AlertDataServiceImpl extends ServiceImpl<AlertDataMapper, AlertData> implements IAlertDataService
{
    @Autowired
    private AlertDataMapper alertDataMapper;
    @Autowired
    private ISysUserService isysUserService;

    /**
     * 查询报警数据
     * 
     * @param id 报警数据主键
     * @return 报警数据
     */
    @Override
    public AlertData selectAlertDataById(Long id)
    {
        return getById(id);
    }

    /**
     * 查询报警数据列表
     * 
     * @param alertData 报警数据
     * @return 报警数据
     */
    @Override
    public List<AlertData> selectAlertDataList(AlertData alertData)
    {
        return alertDataMapper.selectAlertDataList(alertData);
    }

    /**
     * 新增报警数据
     * 
     * @param alertData 报警数据
     * @return 结果
     */
    @Override
    public int insertAlertData(AlertData alertData)
    {
        return save(alertData) ? 1 : 0;
    }

    /**
     * 修改报警数据
     * 
     * @param alertData 报警数据
     * @return 结果
     */
    @Override
    public int updateAlertData(AlertData alertData)
    {
        return updateById(alertData) ? 1 : 0;
    }

    /**
     * 批量删除报警数据
     * 
     * @param ids 需要删除的报警数据主键
     * @return 结果
     */
    @Override
    public int deleteAlertDataByIds(Long[] ids)
    {
        return removeByIds(Arrays.asList(ids)) ? 1 : 0;
    }

    /**
     * 删除报警数据信息
     * 
     * @param id 报警数据主键
     * @return 结果
     */
    @Override
    public int deleteAlertDataById(Long id)
    {
        return removeById(id) ? 1 : 0;
    }

    /**
     *
     * @param devicename
     * @param startTime
     * @param endTime
     * @param id
     * @param status
     * @return
     */
    @Override
    public List<AlertData> querylistbypage(String devicename, LocalDateTime startTime, LocalDateTime endTime, Integer id, Integer status) {
        LoginUser loginUser = SecurityUtils.getLoginUser();
        Long userId = loginUser.getUserId();
        LambdaQueryWrapper<AlertData> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(StrUtil.isNotEmpty(devicename),AlertData::getDeviceName, devicename)
                .between(ObjectUtil.isNotNull(startTime)&&ObjectUtil.isNotNull(endTime), AlertData::getCreateTime, startTime, endTime)
                .eq( AlertData::getUserId, userId)
                .eq(ObjectUtil.isNotEmpty(status), AlertData::getStatus, status);
        List<AlertData> alertDatalist = list(wrapper);
        return alertDatalist;
    }

    /**
     * 处理报警数据
     * @param alertDataDto
     */
    @Override
    public void handleAlertData(AlertDataDto alertDataDto) {
        // 获取当前处理人的id
        LoginUser loginUser = SecurityUtils.getLoginUser();
        Long userId = loginUser.getUserId();
        // 根据处理人id获取处理人数据
        SysUser sysUser = isysUserService.selectUserById(userId);
        alertDataDto.setProcessorName(sysUser.getNickName());
        alertDataDto.setProcessorId(userId);

        // 根据报警数据id查询报警规则id
        AlertData alertData = alertDataMapper.selectOne(Wrappers.<AlertData>lambdaQuery()
                .eq(AlertData::getId, alertDataDto.getId()));

        // 设置处理状态为已处理
        alertDataDto.setStatus(1);
        LambdaQueryWrapper<AlertData> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(AlertData::getAlertRuleId, alertData.getAlertRuleId());
        AlertData newAlertData = BeanUtil.toBean(alertDataDto, AlertData.class);
        alertDataMapper.update(newAlertData, wrapper);
    }
    }



